package com.casems.commons.util;

import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;

public class JwtUtil {

    private static final String KEY = "CASESMS";
    private static final long EXPIRE = 1000 * 60 * 1000;

    //创建token
    public static String createToken(String phone){
        Date now = new Date();
        return Jwts.builder().signWith(SignatureAlgorithm.HS256,KEY)
                .setSubject(phone)
                .setIssuedAt(now)//创建时间
                .setExpiration(new Date(now.getTime() + EXPIRE))//过期时间
                .compact();
    }
    /**
     * phone number
     *
     * @param jwt
     * @return
     */
    public static String getPhone(String jwt) {
        return Jwts.parser()
                .setSigningKey(KEY)
                .parseClaimsJws(jwt)
                .getBody()
                .getSubject();
    }
}
